package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.item.entity.Brand;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.IBrandService;
import com.leyou.test.enums.ExceptionEnum;
import com.leyou.test.exceptions.LyException;
import com.leyou.utils.BeanHelper;
import com.leyou.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService{

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌分页查询
     *
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @Override
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        //分页
        PageHelper.startPage(page, rows);
        //条件
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //如果查询关键字key不为空,添加查询条件
        if (!StringUtils.isBlank(key)) {
            criteria.orLike("name", "%" + key + "%")
            .orEqualTo("letter",key.toUpperCase());
        }
        //如果排序字段sortBy不为空,添加排序条件
        if (!StringUtils.isBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }

        //执行查询
        List<Brand> brands = brandMapper.selectByExample(example);

        //如果查询结果为空,抛出异常
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        //解析查询结果
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);

        //转换brandDTO
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        return new PageResult<>(pageInfo.getTotal(), brandDTOS);
    }

    /**
     * 新增品牌
     *
     * @param brandDTO
     * @param cids
     */
    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        //将BrandDTO转换成Brand
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        //新增brand到数据库
        int bCount = brandMapper.insertSelective(brand);
        //如果新增失败,抛出异常
        if (1 != bCount) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //新增category_brand中间关系表数据
        int cbCount = brandMapper.insertCategoryAndBrand(brand.getId(), cids);
        //如果新增失败,抛出异常
        if (cbCount != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 根据id查询brand品牌
     * @param brandId
     * @return
     */
    @Override
    public BrandDTO queryBrandById(Long brandId) {

        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (null == brand) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }


    /**
     * 根据分类id查询品牌
     * @param id
     * @return
     */
    @Override
    public List<BrandDTO> queryBrandsByCategory(Long id) {
        List<Brand> brands = brandMapper.queryBrandsByCategory(id);
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }

    /**
     * 根据bids批量查询品牌
     * @param ids
     * @return
     */
    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> ids) {
        List<Brand> brands = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }
}
